﻿using System;
using System.Collections.Generic;

namespace Cherry.Agile.RpcService.ExtProcessor
{
    /// <summary>
    /// 服务处理跟踪扩展插件
    /// </summary>
    public class TopTenTracer : System.Web.UI.Page, IServiceExtentProcessor
    {


        private static object objLock=new object();
        /// <summary>
        /// 最新处理的10个服务跟踪信息
        /// </summary>
        public Queue<string> LatestTrace = new Queue<string>(10);
        /// <summary>
        /// 最新的10个服务异常处理信息
        /// </summary>
        public Queue<string> LatestError = new Queue<string>(10);

        public void ProcessOnStarting(ProcessContext context)
        {
        }

        public void ProcessOnEnding(ProcessContext context)
        {
        }

        public void ProcessTrace(Exception ex, ProcessContext context)
        {
            string msg = string.Format("{0:yyyy-MM-dd HH:mm:ss} {1} - {2}", DateTime.Now, ex.Message, ex.StackTrace);
            lock (objLock)
            {
                LatestError.Enqueue(msg);
                if (LatestError.Count > 10)
                    LatestError.Dequeue();
            }
        }

        public void ProcessTrace(string traceMessage, ProcessContext context)
        {
            lock (objLock)
            {
                LatestTrace.Enqueue(traceMessage);
                if (LatestTrace.Count > 10)
                    LatestTrace.Dequeue();
            }
        }
    }
}